angular.module('hyt.loginService', ['ngResource'])

  .factory('Storage', function() {
    return {
      set: function(key, data) {
        return window.localStorage.setItem(key, window.JSON.stringify(data));
      },
      get: function(key) {

        return window.JSON.parse(window.localStorage.getItem(key));
      },
      remove: function(key) {
        return window.localStorage.removeItem(key);
      }
    };
  })

  .factory('Register', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.registUser,
      result;
    var resource = $resource(ApiUrl);
    return{
      getRegistUser : function(registUser){
        return resource.save({
          username:registUser.username,
          password:registUser.password,
          cname:registUser.username
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Register.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })

  .factory('VerificationCode',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.getVerificationCode;

    var resource = $resource(ApiUrl+':username',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getVerificationCode : function(username){
        return resource.query({
          username:username
        },function(r){
          console.log(r);
        })
      }
    }
  })

  .factory('Verification',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.checkVerificationCode,
      result;

    var resource = $resource(ApiUrl+':verification',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      checkVerificationCode : function(verification){
        return resource.query({
          verification:verification
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('Verification.Updated');
        })
      },
      getResult : function(){
        return result;
      }
    }
  })

  .factory('Login',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.login,
      result;

    var resource = $resource(ApiUrl+':user',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getLogin : function(user){
        return resource.query({
          user:user
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('Login.Updated');
        })
      },
      getResult : function(){
        return result;
      }
    }
  })

  .factory('ValidLogin',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.validLogin,
      result;

    var resource = $resource(ApiUrl+':validLogin',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getValidLogin : function(validLogin){
        return resource.query({
          validLogin:validLogin
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('ValidLogin.Updated');
        })
      },
      getResult : function(){
        return result;
      }
    }
  })
  .factory('ForgetPwd',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.forgetPwd,
      result;

    var resource = $resource(ApiUrl+':forgetPwd',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getForgetPwd : function(forgetPwd){
        console.log(forgetPwd);
        return resource.query({
          forgetPwd:forgetPwd
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('ForgetPwd.Updated');
        })
      },
      getResult : function(){
        return result;
      }
    }
  })
  .factory('CountExist',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.isCountExist,
      result;

    var resource = $resource(ApiUrl+':countExist',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getCountExist : function(countExist){
        return resource.query({
          countExist:countExist
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('CountExist.Updated');
        })
      },
      getResult : function(){
        return result;
      }
    }
  })
